﻿using System;
using System.Collections.Generic;
using System.Collections;
using AppContext = T.ServiceComponent.Session.Service.AppContext;

namespace T.FAS.Runtime.DataAccess.Common
{
    /// <summary>
    /// 查询条件工具类
    /// </summary>
    public class QueryUtil
    {
        /// <summary>
        /// 校验分页参数
        /// </summary>
        /// <param name="condition">
        /// Key分别为：offset
        /// </param>
        public static void CheckPageParam(Hashtable condition)
        {
            int pageNumber, rows;


            if (int.TryParse(Convert.ToString(condition["page"]), out pageNumber) &&
                int.TryParse(Convert.ToString(condition["rows"]), out rows))
            {
                condition["offset"] = (pageNumber - 1) * rows;
            }
            else
            {
                condition["offset"] = 0;


                //LogUtil.Log(string.Format("分页参数不正确:page:{0},rows:{1}", condition["page"], condition["rows"]));
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="condition"></param>
        public static void CheckPageParam(Dictionary<string, object> condition)
        {
            if (!condition.ContainsKey("page") || !condition.ContainsKey("rows"))
            {
                return;
            }

            int pageNumber, rows;
            if (int.TryParse(Convert.ToString(condition["page"]), out pageNumber) &&
                int.TryParse(Convert.ToString(condition["rows"]), out rows))
            {
                condition["offset"] = (pageNumber - 1) * rows;
            }
            else
            {
                condition["offset"] = 0;

                //LogUtil.Log(string.Format("分页参数不正确:page:{0},rows:{1}", condition["page"], condition["rows"]));
            }
        }
    }


    /// <summary>
    /// Session工具
    /// </summary>
    public class UserUtil
    {
        /// <summary>
        /// 获取当前登录的用户ID
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentUserID()
        {
            string userID;
            try
            {
                if (AppContext.Current?.Session?["UserId"] == null)
                    return "T";
                else
                    userID = Convert.ToString(AppContext.Current.Session["UserId"]);
            }
            catch
            {
                userID = "T";
            }
            return userID;
        }
        /// <summary>
        /// 获取当前登录用户的名称
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentUserName(string defaultUserName = "")
        {
            string userName;
            try
            {
                if (AppContext.Current?.Session?["UserName"] == null) return "T";
                userName = Convert.ToString(AppContext.Current.Session["UserName"]);
                userName = !String.IsNullOrEmpty(userName) ? userName : Convert.ToString(AppContext.Current.Session["Account"]);
                //如果用户名为空  且 默认用户名不为空 那么采用默认用户名
                if (string.IsNullOrEmpty(userName) == true)
                {
                    if (string.IsNullOrEmpty(defaultUserName) == false)
                    {
                        userName = defaultUserName;
                    }
                    else
                    {
                        userName = "T";
                    }
                }
            }
            catch (Exception ex)
            {
                if (string.IsNullOrEmpty(defaultUserName) == true)
                {
                    userName = "T";
                }
                else
                {
                    userName = defaultUserName;
                }
            }
            return userName;
        }
        /// <summary>
        /// 获取当前登录用户的租户ID
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentUserTenantId()
        {
            string userID;
            try
            {
                if (AppContext.Current?.Session?["CompanyId"] == null) return "T";
                userID = Convert.ToString(AppContext.Current.Session["CompanyId"]);
            }
            catch
            {
                userID = "T";
            }
            return userID;
        }
    }

}
